﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.SceneManagement;

namespace Nirvana
{
    /// <summary>
    /// 场景ab加载携程
    /// </summary>
    public sealed class WaitLoadSceneProgressAsync : WaitLoadAssetProgress
    {
        private AsyncOperation asyncOperation;
        private float time;

#if UNITY_EDITOR
        public WaitLoadSceneProgressAsync(string bundle,string assetname,LoadSceneMode loadSceneMode,float t)
        {
            this.time = t;
            string[] assetnames = AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(bundle, System.IO.Path.GetFileNameWithoutExtension(assetname));
            if(assetnames.Length == 0)
            {
                base.Error = string.Format("There is no scene with name \"{0}\" in \"{1}\"", bundle, assetname);
                return;
            }

            if(loadSceneMode == LoadSceneMode.Additive)
            {
                this.asyncOperation = EditorApplication.LoadLevelAdditiveAsyncInPlayMode(assetnames[0]);
            }
            else
            {
                this.asyncOperation = EditorApplication.LoadLevelAsyncInPlayMode(assetnames[0]);
            }
        }
#endif

        public override float Progress
        {
            get
            {
                return (this.asyncOperation == null) ? 0f : this.asyncOperation.progress;
            }
        }
        /// <summary>
        /// 等待携程中断返回
        /// </summary>
        public override bool keepWaiting
        {
            get
            {
                this.time -= Time.unscaledDeltaTime;
                return this.time > 0f || (this.asyncOperation != null && !this.asyncOperation.isDone);
            }
        }

        public override bool Load()
        {
            return false;
        }

    }
}

